﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web.UI;

/// <summary>
/// Summary description for SessionControlBase
/// </summary>
public class SessionControlBase : System.Web.UI.UserControl
{
	protected bool m_bSupressMessages = false;

	public SessionControlBase()
	{
	}

	private string PageGUID
	{
		get
		{
			if (ViewState["PageGUID"] == null)
				ViewState["PageGUID"] = Guid.NewGuid().ToString();
			return (string)ViewState["PageGUID"];
		}
	}

	public string GetSessionKey(string strTagName)
	{
		return this.PageGUID + "_" + strTagName;
	}

	public void SetSession(string strTagName, object oValue)
	{
		Session[GetSessionKey(strTagName)] = oValue;
	}

	public void ClearSession(string strTagName)
	{
		Session[GetSessionKey(strTagName)] = null;
	}

	public object GetSession(string strTagName)
	{
		return Session[GetSessionKey(strTagName)];
	}

	public DataSet GetSessionDataSet(string strTagName)
	{
		return (DataSet)GetSession(strTagName);
	}

	public string UserName
	{
		get { return (string)(this.ViewState["UserName"] ?? ""); }
		set { this.ViewState["UserName"] = value; }
	}

	public void SetMessage(string strMessage, bool bError)
	{
		if (bError)
			strMessage = "<PRE>" + Server.HtmlEncode(Tools.GetErrorMessage(strMessage)) + "</PRE>";
		strMessage = strMessage.ReplaceNewLineWithBR();
		//if (bError)
		//Tools.ScrieInLog(Server, "ERROR (MasterPage.master.cs) [SessionID='" + Session.SessionID + "', UserName='" + GetUserName() + "', PathAndQuery='" + this.Request.Url.PathAndQuery + "']:\r\nMessage: " + strMessage + "\r\nStackTrace: \r\n" + (new System.Diagnostics.StackTrace()).ToString());
		ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "SetMessage", "DisplayMessage(\"" + strMessage.Replace(@"\", @"\\") + "\", '" + (bError ? "clsError" : "clsMessage") + "');", true);
	}
}
